package com.apanal.qlife.sys.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.util.FileOperateUtil;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.sys.dao.FileMapper;
import com.apanal.qlife.sys.model.File;
import com.apanal.qlife.sys.service.FileService;

/**
 * 文件服务层接口实现类
 * 
 * @author shuliangxing
 * 
 * @date 2015-1-14上午11:36:08
 */
@Service
public class FileServiceImpl implements FileService {

	@Autowired
	FileMapper mapper;

	@Override
	public int deleteById(Long id) {
		return mapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(File record) {
		if (record.getFilId() == null) {
			record.setFilId(IdWorkerFactory.nextId());
		}
		return mapper.insert(record);
	}

	@Override
	public int insertSelective(File record) {
		if (record.getFilId() == null) {
			record.setFilId(IdWorkerFactory.nextId());
		}
		return mapper.insertSelective(record);
	}

	@Override
	public File selectById(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByIdSelective(File record) {
		return mapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateById(File record) {
		return mapper.updateByPrimaryKey(record);
	}

	@Override
	public int deleteSoft(Long id) {
		File entity = new File();
		entity.setFilId(id);
		entity.setStatus(Constants.IS_VALID_FALSE);
		return mapper.updateByPrimaryKeySelective(entity);
	}

	public List<File> uploadFile(HttpServletRequest request, String filType,
			String filTypeName, String typePath) throws IOException {
		List<File> fileList = new ArrayList<File>();
		List<Map<String, Object>> list = FileOperateUtil.upload(
				Constants.FILE_UPLOAD_PATH, typePath, request);
		for (Map<String, Object> map : list) {
			if (map != null) {
				File file = new File();
				file.setFilOriName((String) map.get(FileOperateUtil.REALNAME));
				file.setFilOriSizeB(Long.valueOf(
						String.valueOf(map.get(FileOperateUtil.SIZE)))
						.intValue());
				file.setFilOriSuffix((String) map.get(FileOperateUtil.SUFFIX));
				file.setFilSerName((String) map.get(FileOperateUtil.STORENAME));
				file.setFilSerPath((String) map.get(FileOperateUtil.STOREPATH));
				file.setFilType(filType);
				file.setFilSerSizeB(Long.valueOf(
						String.valueOf(map.get(FileOperateUtil.SIZE)))
						.intValue());
				file.setFilTypeName(filTypeName);
				file.setInsertDate(new Date());
				file.setInsertBy((long) 1);
				file.setLastUpdateBy((long) 1);
				file.setLastUpdateDate(new Date());
				fileList.add(file);
			}
		}

		return fileList;
	}

	@Override
	public List<File> select(String filType, Long foreignId) {
		File file = new File();
		file.setFilType(filType);
		file.setForeignId(foreignId);
		return mapper.selectRelation(file);
	}

	@Override
	public String deleteByForeignId(Long foreignId) {
		mapper.deleteByForeignId(foreignId);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	@Override
	public int inserts(List<File> list) {
		if (list == null || list.isEmpty()) {
			return 0;
		}

		for (File file : list) {
			if (file.getFilId() == null) {
				file.setFilId(IdWorkerFactory.nextId());
			}
		}

		return mapper.inserts(list);
	}

}
